home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / Pascal / SWar / Util.p < prev   
Text File  |  1995-06-15  |  3KB  |  148 lines

  1. unit Util;
  2.  
  3. interface
  4.  
  5.     uses
  6. {$IFC UNDEFINED THINK_PASCAL}
  7.         Types, QuickDraw, Events, Menus, Dialogs, Fonts, Resources, Devices, ToolUtils, SegLoad,
  8. {$ENDC}
  9.         Globals, SWSound;
  10.  
  11.     function AbsVal (arg: Integer): Integer; {Use "abs" instead}
  12.     function RngRnd (min: Integer; max: Integer): Integer;
  13.     function IsPressed (keyID: Integer): Boolean;
  14.     procedure SetChecks;
  15.     procedure LoadSounds;
  16.     procedure UnloadSounds;
  17.     procedure LoadSound (var soundH: Handle; soundID: Integer; soundName: Str255);
  18.     procedure UnloadSound (soundH: Handle);
  19.     procedure ExitAppl;
  20.     procedure ErasePort (whichPort: CGrafPtr; whatRect: Rect);
  21.     function ColorsEqual (rgbc1, rgbc2: RGBColor): Boolean;
  22.  
  23. implementation
  24.  
  25.     function AbsVal (arg: Integer): Integer;
  26.     begin
  27.         if arg < 0 then
  28.             AbsVal := -arg
  29.         else
  30.             AbsVal := arg;
  31.     end; (* AbsVal() *)
  32.  
  33.     function IsPressed (keyID: Integer): Boolean;
  34.         var
  35.             keys: KeyMap;
  36.     begin
  37.         GetKeys(keys);
  38.         IsPressed := keys[keyID];
  39.     end; (* IsPressed() *)
  40.  
  41.     function RngRnd (min: Integer; max: Integer): Integer;
  42.         var
  43.             rnd: Integer;
  44.             range, t: LongInt;
  45.     begin
  46.         rnd := BitAnd(Random, $7fff);
  47.         range := max - min;
  48.         t := BSR(rnd * range, 15);
  49.         RngRnd := (t + min);
  50.     end; (* RngRnd() *)
  51.  
  52.  
  53.     procedure SetChecks;
  54.     begin
  55.         CheckItem(gFileMenu, 2, gPauseOn);
  56.         CheckItem(gFileMenu, 3, gSoundOn);
  57.     end; (* SetChecks() *)
  58.  
  59.     procedure LoadSound (var soundH: Handle; soundID: Integer; soundName: Str255);
  60.     begin
  61.         soundH := GetResource('snd ', soundID);
  62.         if soundH = nil then
  63.             begin
  64.                 ExitAppl;
  65.             end; (* if *)
  66.         MoveHHi(soundH);
  67.         HLock(soundH);
  68.         HNoPurge(soundH);
  69.     end; (* LoadSound() *)
  70.  
  71.     procedure UnloadSound (soundH: Handle);
  72.     begin
  73.         HUnlock(soundH);
  74.         HPurge(soundH);
  75.         ReleaseResource(soundH);
  76.     end; (* UnloadSound() *)
  77.  
  78.  
  79.     procedure LoadSounds;
  80.  
  81. {     evar Handle            gNewMatchSoundH:xtern;}
  82. {    eHandle            gShotSoundH:xtern;}
  83. {    eHandle            gBoomSoundH:xtern;}
  84. {    eHandle            gEndorsementSndH:xtern;}
  85.  
  86.     begin
  87.         LoadSound(gNewMatchSoundH, 501, '''snd '' New Level (501)');
  88.         LoadSound(gShotSoundH, 502, '''snd'' Shot (502)');
  89.         LoadSound(gBoomSoundH, 503, '''snd'' Boom (503)');
  90.         AInitSnd;
  91.     end; (* LoadSounds() *)
  92.  
  93.     procedure UnloadSounds;
  94. {     evar Handle            gNewMatchSoundH:xtern;}
  95. {    eHandle            gShotSoundH:xtern;}
  96. {    eHandle            gBoomSoundH:xtern;}
  97. {    eHandle            gEndorsementSndH:xtern;}
  98.     begin
  99.         UnloadSound(gNewMatchSoundH);
  100.         UnloadSound(gShotSoundH);
  101.         UnloadSound(gBoomSoundH);
  102.         AStopSnd;
  103.  
  104.     end; (* UnloadSounds() *)
  105.  
  106.  
  107.     procedure ExitAppl;
  108.     begin
  109.         if gPictureWindow <> nil then
  110.             DisposeWindow(WindowPtr(gPictureWindow));
  111.         if (gOSPtr <> nil) then
  112.             begin
  113.                 CloseCPort(gOSPtr);
  114.                 DisposePtr(Ptr(gOSPtr));
  115.             end; (* if *)
  116.         if (gScrapPtr <> nil) then
  117.             begin
  118.                 CloseCPort(gScrapPtr);
  119.                 DisposePtr(Ptr(gScrapPtr));
  120.             end; (* if *)
  121.         ShowCursor;
  122.         FlushEvents(everyEvent, 0);
  123.         UnloadSounds;
  124.         ExitToShell;
  125.     end; (* ExitAppl() *)
  126.  
  127.     procedure ErasePort (whichPort: CGrafPtr; whatRect: Rect);
  128.         var
  129.             savePort: GrafPtr;
  130.     begin
  131.         GetPort(savePort);
  132.         SetPort(GrafPtr(whichPort));
  133.         EraseRect(whatRect);
  134.         SetPort(savePort);
  135.     end; (* ErasePort() *)
  136.  
  137.     function ColorsEqual (rgbc1, rgbc2: RGBColor): Boolean;
  138.     begin
  139.         ColorsEqual := true;
  140.         if (rgbc1.red <> rgbc2.red) then
  141.             ColorsEqual := FALSE
  142.         else if (rgbc1.green <> rgbc2.green) then
  143.             ColorsEqual := FALSE
  144.         else if (rgbc1.blue <> rgbc2.blue) then
  145.             ColorsEqual := FALSE;
  146.     end; (* ColorsEqual() *)
  147.  
  148. end.